package cn.itlight.order.service.impl;

import cn.itlight.basic.exception.BusinessException;
import cn.itlight.basic.utils.BaiduAuditUtils;
import cn.itlight.basic.utils.CodeGenerateUtils;
import cn.itlight.basic.utils.DistanceUtil;
import cn.itlight.basic.utils.Point;
import cn.itlight.order.controller.OrderProductAcquisitionController;
import cn.itlight.order.domain.OrderPetAcquisition;
import cn.itlight.order.domain.OrderProduct;
import cn.itlight.order.domain.OrderProductAcquisition;
import cn.itlight.order.domain.OrderProductDetail;
import cn.itlight.order.mapper.OrderProductAcquisitionMapper;
import cn.itlight.order.mapper.OrderProductDetailMapper;
import cn.itlight.order.mapper.OrderProductMapper;
import cn.itlight.order.service.IOrderProductService;
import cn.itlight.basic.service.impl.BaseServiceImpl;
import cn.itlight.org.domain.Shop;
import cn.itlight.pet.domain.Pet;
import cn.itlight.pet.domain.SearchMasterMsg;
import cn.itlight.pet.domain.SearchMasterMsgAuditLog;
import cn.itlight.pet.dto.HandlerMsgDto;
import cn.itlight.product.domain.Product;
import cn.itlight.product.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tomliu
 * @since 2022-07-21
 */
@Service
public class OrderProductServiceImpl extends BaseServiceImpl<OrderProduct> implements IOrderProductService {

    @Autowired
    private OrderProductMapper orderProductMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private OrderProductDetailMapper orderProductDetailMapper;

    @Autowired
    private OrderProductAcquisitionMapper orderProductAcquisitionMapper;
    @Override
    public void shopping(OrderProduct orderProduct) {
        // 查询出产品信息
        Product product = productMapper.loadById(orderProduct.getProductId());
        // 保存订单
        orderProduct.setDigest("[服务订单]购买1次" + product.getName());
        orderProduct.setState(0);
        orderProduct.setPrice(product.getSaleprice());
        orderProduct.setOrderSn(CodeGenerateUtils.generateOrderSn(orderProduct.getUserId()));
        orderProduct.setLastPayTime(new Date(System.currentTimeMillis() + 2 * 24 * 60 * 60 * 1000));
        orderProduct.setNum(1);
        System.out.println(orderProduct);
        orderProductMapper.save(orderProduct);

        // 保存订单详情表
        OrderProductDetail orderProductDetail = new OrderProductDetail();
        orderProductDetail.setName(product.getName());
        orderProductDetail.setResources(product.getResources());
        orderProductDetail.setSaleprice(product.getSaleprice());
        orderProductDetail.setOffsaletime(product.getOffsaletime());
        orderProductDetail.setOnsaletime(product.getOnsaletime());
        orderProductDetail.setState(1L);
        orderProductDetail.setCostprice(product.getCostprice());
        orderProductDetail.setProductId(product.getId());
        orderProductDetail.setOrderId(orderProduct.getId());
        orderProductDetailMapper.save(orderProductDetail);
    }

    @Override
    public void accept(HandlerMsgDto handlerMsgDto) {
        // 根据msgid和订单是否是取消状态查询订单
        OrderProductAcquisition orderProductAcquisition = orderProductAcquisitionMapper.loadByMsgIdAndNotCanceled(handlerMsgDto.getMsgId());
        // 如果查询到了收购单，那么说明此消息已被接单，提示用户
        if (orderProductAcquisition != null){
            throw new BusinessException("此消息已被接单！");
        }
        //  2.生成宠物基本信息，详情数据可以等店铺上架时，再进行生成
        OrderProduct orderProduct = orderProductMapper.loadById(handlerMsgDto.getMsgId());

        // 3.生成收购单信息
        // initOrderPetAcquisition(pet);
        orderProductAcquisition = initOrderPetAcquisition(handlerMsgDto, orderProduct);
        orderProductAcquisitionMapper.save(orderProductAcquisition);
    }

    @Override
    public void reject(Long id) {
        // 1.将寻主消息所绑定的店铺Id设置为null
        OrderProduct orderProduct = orderProductMapper.loadById(id);
        if (orderProduct == null){
            throw new BusinessException("此订单不存在！");
        }
        orderProductMapper.remove(id);
    }

    private OrderProductAcquisition initOrderPetAcquisition(HandlerMsgDto handlerMsgDto, OrderProduct orderProduc) {

        OrderProductAcquisition orderProductAcquisition = new OrderProductAcquisition();
        orderProductAcquisition.setOrderSn(orderProduc.getOrderSn());
        orderProductAcquisition.setDigest(orderProduc.getDigest());
        orderProductAcquisition.setLastcomfirmtime(new Date(System.currentTimeMillis() + 2 * 24 * 60 * 60 * 1000));
        orderProductAcquisition.setPrice(orderProduc.getPrice());
        orderProductAcquisition.setUserId(orderProduc.getUserId());
        orderProductAcquisition.setShopId(orderProduc.getShopId());
        orderProductAcquisition.setEmpId(handlerMsgDto.getHandler());
        orderProductAcquisition.setTOrderProductId(handlerMsgDto.getMsgId());
        return orderProductAcquisition;
    }
}
